<template>
  <div class="activity-add-container">
    <el-form ref="form" :rules="option.rules" label-width="80px">
      <el-form-item label="社团名称">
        <el-input v-model="form.communityName"/>
      </el-form-item>
      <el-form-item label="社团标签">
        <el-input v-model="form.communityTags"/>
      </el-form-item>
      <el-form-item label="社团描述">
        <el-input v-model="form.communityDesc" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="社团地址">
        <b-map @confirm-location="confirmLocation"></b-map>
      </el-form-item>
      <el-form-item>
        <el-upload
            :before-remove="beforeRemove"
            :file-list="option.fileList"
            :http-request="handleUpload"
            :limit="1"
            :on-exceed="handleExceed"
            :on-remove="handleRemove"
            action
            class="upload-demo"
            list-type="picture-card"
            name="singleFile"
        >
          <i class="el-icon-plus"></i>
          <div slot="tip" class="el-upload__tip">最多上传一张社团团徽</div>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="toSubmit(form,parentcommunityId)">立即添加</el-button>
        <el-button type="primary" @click="handleCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  props: ["parentcommunityId"],

  components: {
    "b-map": () => import("../../components/map/bMap"),
  },
  data () {
    return {
      form: {
        "communityAddress": "",
        "communityDesc": "",
        "communityImage": "",
        "communityName": "",
        "communityTags": "",
      },
      option: {
        fileList: [],
      },
    };
  },
  methods: {
    // 立即发布
    async toSubmit (g, id) {
      for (let i in g) if (!g[i]) return this.$message.warning("请补充完整相关信息");
      const params = {
        "communityAddress": g.communityAddress,
        "communityDesc": g.communityDesc,
        "communityImage": g.communityImage,
        "communityName": g.communityName,
        "communityTags": g.communityTags,
        "parentcommunityId": id,
      };
      // console.log(params)
      const res = await this.$http.community_registry(params);
      if (res?.code !== 0) this.$message.error("创建失败");
      return this.$router.go(0);
    },

    confirmLocation (location) {
      // 确认位置信息
      this.form.communityAddress = JSON.stringify(location);

    },
    // 移除文件
    handleRemove (file, fileList) {
    },
    handleExceed (files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove (file) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    // 上传文件
    async handleUpload (file) {
      const params = new FormData();
      params.append("files", file.file);
      const res = await this.$http.uploadFile(params);
      if (res.code !== 200) return this.$message.error(res.message);
      this.form.communityImage = res.files[0];
    },
    handleCancel (e) {
      this.$emit("cancel", e);
    },

  },
};
</script>
